<?php 
require("../db/opendb.php");

//echo "reco_recvr_id=";
//echo $_POST['ID'];
//echo "<br>";

$from_email = urldecode($_GET['from_email']);
$from_fname = urldecode($_GET['from_fname']);
$from_lname = urldecode($_GET['from_lname']);
$recommendation = urldecode($_GET['recoresponse']);
$for_username = urldecode($_GET['for_username']);
$for_userid = $_GET['for_userid'];
$for_email = urldecode($_GET['for_email']);
$for_fname = urldecode($_GET['for_fname']);
$for_lname = urldecode($_GET['for_lname']);
$for_site_url = urldecode($_GET['for_site_url']);
$for_site_url_login = "http://".urldecode($_GET['for_site_url'])."/wp-admin/admin.php?page=Agent_Network&frame=network_recommendations&admin=true";


//echo "from_fname = ".$from_fname."<br>";
//echo "from_lname = ".$from_lname."<br>";
//echo "from_email = ".$from_email."<br>";
//echo "recommendation = ".$recommendation."<br>";
//echo "for_username = ".$for_username."<br>";
//echo "for_userid = ".$for_userid."<br>";

//echo "for_fname = ".$for_fname."<br>";
//echo "for_lname = ".$for_lname."<br>";
//echo "for_site_url = ".$for_site_url."<br>";

if($recommendation == '')
{
$errorcode = 8;

$xml_output.= "<entries>\n"; 
$xml_output.= "\t<entry>\n";   
$xml_output.= "\t<errorcode><![CDATA[" . $errorcode . "]]></errorcode>\n"; 
$xml_output.= "\t</entry>\n";
$xml_output .= "</entries>";  

echo $xml_output;
return;

}

if($from_fname == '' && $from_lname == '' && $from_email == ''){
$errorcode = 7;

$xml_output.= "<entries>\n"; 
$xml_output.= "\t<entry>\n";   
$xml_output.= "\t<errorcode><![CDATA[" . $errorcode . "]]></errorcode>\n"; 
$xml_output.= "\t</entry>\n";
$xml_output .= "</entries>";  

echo $xml_output;
return;
}

if($from_fname == '' && $from_lname == '' && $from_email != ''){
$errorcode = 4;

$xml_output.= "<entries>\n";
$xml_output.= "\t<entry>\n";    
$xml_output.= "\t<errorcode><![CDATA[" . $errorcode . "]]></errorcode>\n"; 
$xml_output.= "\t</entry>\n";
$xml_output .= "</entries>";  

echo $xml_output;
return;
}

if($from_fname == '' && $from_lname != '' && $from_email == ''){
$errorcode = 5;

$xml_output.= "<entries>\n"; 
$xml_output.= "\t<entry>\n";   
$xml_output.= "\t<errorcode><![CDATA[" . $errorcode . "]]></errorcode>\n"; 
$xml_output.= "\t</entry>\n";
$xml_output .= "</entries>";  

echo $xml_output;
return;
}

if($from_fname != '' && $from_lname == '' && $from_email == ''){
$errorcode = 6;

$xml_output.= "<entries>\n"; 
$xml_output.= "\t<entry>\n";   
$xml_output.= "\t<errorcode><![CDATA[" . $errorcode . "]]></errorcode>\n"; 
$xml_output.= "\t</entry>\n";
$xml_output .= "</entries>";  

echo $xml_output;
return;
}

if($from_fname == ''){
$errorcode = 2;

$xml_output.= "<entries>\n"; 
$xml_output.= "\t<entry>\n";   
$xml_output.= "\t<errorcode><![CDATA[" . $errorcode . "]]></errorcode>\n"; 
$xml_output.= "\t</entry>\n";
$xml_output .= "</entries>";  

echo $xml_output;
return;
}

if($from_lname == ''){
$errorcode = 3;

$xml_output.= "<entries>\n";
$xml_output.= "\t<entry>\n";   
$xml_output.= "\t<errorcode><![CDATA[" . $errorcode . "]]></errorcode>\n"; 
$xml_output.= "\t</entry>\n";
$xml_output .= "</entries>";  

echo $xml_output;
return;
}

if($from_email == ''){
$errorcode = 1;

$xml_output.= "<entries>\n";
$xml_output.= "\t<entry>\n";  
$xml_output.= "\t<errorcode><![CDATA[" . $errorcode . "]]></errorcode>\n"; 
$xml_output.= "\t</entry>\n";
$xml_output .= "</entries>";  

echo $xml_output;
return;
}


$esql = "Select ID, Username, First_Name, Last_Name, Email from Users  where Email = '".$from_email."'";
$eresult = mysql_query($esql) or die ("SQL Error".mysql_error());


$erow = mysql_fetch_assoc($eresult);

if($erow == '')
{
//echo "No user found";
$new_user = 1;
$verified = 1;

$insertquery = "insert into Users(";
$insertquery .= "Username,";
$insertquery .= "Password,";
$insertquery .= "First_Name,";
$insertquery .= "Last_Name,";
$insertquery .= "Date_Created,";
$insertquery .= "Verified,";
$insertquery .= "Email)";
//$insertquery .= "Street,";
//$insertquery .= "City,";
//$insertquery .= "State,";
//$insertquery .= "Zip)";
$insertquery .= "Values(";
$insertquery .= "'".$from_email."',";
$insertquery .= "'".md5($firstname)."',";
$insertquery .= "'".$from_fname."',";
$insertquery .= "'".$from_lname."',";
$insertquery .= "'".date("Y-m-d")."',";
$insertquery .= "'".$verified."',";
$insertquery .= "'".$from_email."')";


mysql_query($insertquery) or die(mysql_error());
$new_id = mysql_insert_id();

}
else
{
$new_user = 0;
$new_id = $erow['ID'];

}

//echo "sql = ".$esql."<br>";
//echo "eresult = ".$eresult;
//print_r($eresult);
//echo "<br>";
//echo "erow =  ";
//print_r($erow);
//echo "<br>";


//$sql = "Select r.Message, r.ID as r_id, u.Username, u.First_Name, u.ID as user_id, u.Last_Name from Recommend_Request r left join Users u on u.ID = r.Owner where r.Recommender = '".$_SESSION['id']."' and r.Status = '0' and r.ID = '".$_POST['user_id']."'";
//$result = mysql_query($sql) or die ("SQL Error".mysql_error());
//$row = mysql_fetch_assoc($result);
//
//$insertquery = "update Recommend_Request set Status = 1 where ID = '".$row['r_id']."'";
//mysql_query($insertquery) or die ("SQL Error".mysql_error());
//
$insertquery = "insert into Recommendations(Owner, Other_User, Message, Date_Created, Status) VALUES ('".$new_id."','".$for_userid."','".stripslashes($recommendation)."','".date('Y-m-d H-i-s')."', 0)";

//echo "reco_query = ".$insertquery."<br>";

mysql_query($insertquery) or die ("SQL Error".mysql_error());
$insert_id = mysql_insert_id();

	$name_reco_giver = $from_fname." ".$from_lname;
	$name_reco_recepient = $for_fname." ".$for_lname;
	$toemail= $for_email;
	
$resql = "Select disp_profile from Users  where Email = '".$for_email."'";
$reresult = mysql_query($resql) or die ("SQL Error".mysql_error());


$rerow = mysql_fetch_assoc($reresult);

if($rerow != '')
{
$no_email = $rerow['disp_profile'];
}

if($no_email != 'temp'){
	$mail_message = "<div style='font-size:16px;'>You have received a recommendation from : <a style='font-size:16px;' href=http://www.realivent.com/user_profile.php?id=".$new_id.">".$name_reco_giver."</a></div><BR>";

//echo "mail message = ".$mail_message."<br>";
	
	$mail_message .= "<div style='font-size:16px;'>You can accept the recommendation by logging into your site at: <a style='font-size:16px;' href=".$for_site_url_login.">".$for_site_url."</a> </div><BR>";

//echo "mail message = ".$mail_message."<br>";

	$mail_message .= "<div style='font-size:16px;'>You can also choose to accept the recommendation by accesing your Realivent Account at: <a style='font-size:16px;' href=http://www.realivent.com/accounts/recommendations.php>Check My New Recommendation on Realivent</a> </div><BR>";

//echo "to email addr = ".$toemail."<br>";
//echo "mail message = ".$mail_message."<br>";

	//$mail_message .= "<BR><BR>You can accept the recommendation by logging into your site at <a style='font-size:16px;' href='".$for_site_url."'></a>  or by accesing your Realivent Account at <a style='font-size:16px;' href=http://www.realivent.com/new_login.php></a>";


	$mheaders = "MIME-Version: 1.0\n";
	$mheaders .= "Content-type: text/html; charset=iso-8859-1\n";
	$mheaders .= "To: $name <$toemail>\n";
	$mheaders .= "From: $name_reco_giver <$from_email>\n";

	$subject = 'Received a recommendation from ';
	$subject .= 'Realivent User: ' . $name_reco_giver;

	$mail_body = $mail_message;
	mail($toemail, $subject, $mail_body, $mheaders);

}

	$mail_message2 = "<div style='font-size:16px;'>Thank you for sending a recommendation for : <a style='font-size:16px;' href=http://www.realivent.com/user_profile.php?id=".$for_userid.">".$name_reco_recepient.", a Realivent patron.</a></div><BR>";

	
if($new_user == 1){
	$mail_message2 .= "<div style='font-size:16px;'>  You can also take advantage of Realivent's multi-dimensional marketing platform for real estate professionals and consumers.  Please login with Username: <strong> ".$from_email."  </strong> with Password: <strong> ".$from_fname." </strong> and explore and enjoy! Start here: <a style='font-size:16px;' href=http://www.realivent.com/new_login.php>The Realivent Platform</a> </div><BR>";
	}
	else{
		$mail_message2 .= "<div style='font-size:16px;'>  You can also take advantage of Realivent's many new offerings for real estate professionals and consumers.  Please login at:  <a style='font-size:16px;' href=http://www.realivent.com/new_login.php>The Realivent Platform</a> </div><BR>";

	}

	$mheaders2 = "MIME-Version: 1.0\n";
	$mheaders2 .= "Content-type: text/html; charset=iso-8859-1\n";
	$mheaders2 .= "To: $name <$from_email>\n";
	$mheaders2 .= "From: $name_reco_recepient <$for_email>\n";

	$subject2 = 'Thank you for your recommendation for ';
	$subject2 .= 'Realivent User: ' . $name_reco_recepient;

	$mail_body2 = $mail_message2;
	mail($from_email, $subject2, $mail_body2, $mheaders2);

$errorcode = 0;

$xml_output.= "<entries>\n";
$xml_output.= "\t<entry>\n";  
$xml_output.= "\t<errorcode><![CDATA[" . $errorcode . "]]></errorcode>\n"; 
$xml_output.= "\t</entry>\n";
$xml_output .= "</entries>";  

echo $xml_output;



?>
<?php require("../db/closedb.php");?>